package com.wtwd.wtwddevice.service.impl;


import com.wtwd.device.constant.Constant;
import com.wtwd.wtwddevice.mapper.DeviceWPhoneMapper;
import com.wtwd.wtwddevice.model.entity.DeviceWPhone;
import com.wtwd.wtwddevice.service.IDeviceWPhoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-06-12
 */
@Service
public class DeviceWPhoneServiceImpl extends ServiceImpl<DeviceWPhoneMapper, DeviceWPhone> implements IDeviceWPhoneService {


    @Resource
    private DeviceWPhoneMapper deviceWPhoneMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean synchronization(Long deviceId, String number, List<DeviceWPhone> deviceWPhones) {
        if (deviceWPhones==null||deviceWPhones.size()==0){
            return tagDeletion(deviceId,number);
        }
        deleteByDeviceId(deviceId);
        boolean rest= deviceWPhoneMapper.batchSave(deviceWPhones)>0?true:false;
        return rest;
    }

    @Override
    public boolean tagDeletion(Long deviceId, String number) {
        if (deviceId==null){
            return false;
        }
        return deviceWPhoneMapper.updateRemoveByDeviceId(deviceId,number, Constant.STAY_REMOVE)>0?true:false;
    }

    @Override
    public boolean deleteByDeviceId(Long deviceId) {
        if (deviceId==null){
            return false;
        }
        return deviceWPhoneMapper.deleteByDeviceId(deviceId)>0?true:false;
    }

    @Override
    public boolean isTagDeletion(Long deviceId, String number) {
        return deviceWPhoneMapper.isTagDeletion(deviceId,number,Constant.STAY_REMOVE)>0?true:false;
    }

    @Override
    public boolean updateState(Long deviceId, String number, int state) {
        return deviceWPhoneMapper.updateState(deviceId,number,state);
    }
}
